home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: const char??
- Date: 25 Feb 1996 12:09:20 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
- References: <31287436.1235873@news.inforamp.net> <4gb7u8$p5o@sun001.spd.dsccc.com> <TANMOY.96Feb20095538@qcd.lanl.gov>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <TANMOY.96Feb20095538@qcd.lanl.gov>,
- Tanmoy Bhattacharya <tanmoy@qcd.lanl.gov> wrote:
- >const is an attribute of the type of an lvalue: When an lvalue with
- >attribute const is used, it means that the code will not change the
- >object using _that_ lvalue.
- >
- >The difference is crucial to understanding function prototypes: when
- >we say that a function is declared `void f(const char *x)', we mean
- >that the function f will not change the object through the lvalue
- >`*x'. The same function may access a global `char y', and it might
- >change y if it feels like, and if you had called f(&y), you can see
- >that *x also got changed by this action!
-
- Precisely. Note that this can also be understood from a purely syntactic
- vantage point:
-
- In the declaration "const char *x", the "const" is a modifier of the type
- "char". The "*x" is a separate syntactic construct which builds on that type.
-
- In parameters, you can't have lists of further declarators, but if the
- declaration was inside the function body, you could do:
-
- const char *x, y;
-
- First, the base type is established as a "const char", a character object
- that is read-only. x is a pointer to such an object, but x is itself
- modifiable. This means that you can change the value of x to point to any
- character, but you cannot modify the character by dereferencing through x.
- Thus, x = "abc"; would be legal, but *x = 'x' would not be legal regardless of
- where x points. On the other hand, the variable y is just a const char. A read
- only character that can be changed only with an intializer. It is valid to do:
-
- const char y = 'a';
-
- But not valid to have a subsequent assignment to y.
-
-
- The proper syntax for making the pointer itself not modifiable is this: you
- declare the basic type, and then complete with a declartor of a constant
- pointer. Thus an unmodifiable pointer x to a modifiable integer would be:
-
- int * const x;
-
- The trick is to see the declaration of the type, and the further declarators of
- objects derived from the type as separate syntactic units. Immediate
- understanding is thereafter forthcoming. :)
-
- >(There is a related issue that the function may also change *x by
- >using the lvalue *(char*)x. That, and similar tricks, are so bad
- >programming practices, that I won't even comment on them!)
-
- Hmm. Is that not a bit of an error, since the const modifier is being dropped
- in the cast? C allows you to only safely convert pointers of any type to void *
- and back to the same type. I will have to check references whether or not that
- "type" is so restrictive as to include any modifiers like const and volatile.
-
- In any case, the cast does make it explicit that you are trying to voluntarily
- break the const. But in your function declaration you promised that you would
- not touch the object---why go through the bother of making that promise to the
- caller, when you intend to violate it?
-
- >An object _defined_ a (i.e. an object whose storage was allocated by a
- >declaration specifying) const, cannot be portably changed by the
- >program. A compiler can assume it's value won't change unless it is
- >also volatile: in which case, even though the program cannot write to
- >it, the compiler has to assume that its value can change due to unkwon
- >reasons.
-
- A read-only device status register would be an example of such a "volatile
- const" beast, no?
- --
-
-